/**
 * 应用主界面Activity，负责底部导航栏切换和Fragment管理
 */
package com.example.gitpart03;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.gitpart03.fragment.FriendFragment;
import com.example.gitpart03.fragment.InformationFragment;
import com.example.gitpart03.fragment.MeFragment;
import com.example.gitpart03.fragment.RecordFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mainText;          // 首页标签
    private TextView messageText;       // 消息标签
    private TextView cartText;          // 好友标签
    private TextView profileText;       // 我的标签
    private List<TextView> tabList = new ArrayList<>();  // 标签列表

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        initView();//初始化控件

        // 默认显示资讯Fragment
        InformationFragment informationFragmentFragment = new InformationFragment();
        changeFragment(informationFragmentFragment);
    }

    /**
     * 底部导航栏点击事件处理，根据点击标签切换对应的Fragment
     * @param v 点击的视图
     */
    @Override
    public void onClick(View v) {
        int clickTabId = v.getId();
        tabChange(clickTabId); // 更新标签选中状态
        if (clickTabId == R.id.text_main) {
            InformationFragment mainFragment = new InformationFragment();
            changeFragment(mainFragment);
        } else if (clickTabId == R.id.text_cart) {
            FriendFragment friendFragment = new FriendFragment();
            changeFragment(friendFragment);
        } else if (clickTabId == R.id.text_message) {
            RecordFragment recordFragment = new RecordFragment();
            changeFragment(recordFragment);
        } else {
            MeFragment meFragment = new MeFragment();
            changeFragment(meFragment);
        }
    }

    /**
     * 切换当前显示的Fragment
     * @param fragment 要显示的Fragment实例
     */
    private void changeFragment(Fragment fragment) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.layout_content, fragment);
        transaction.commit();
    }

    /**
     * 初始化视图控件，绑定底部导航栏标签并设置点击监听
     */
    private void initView() {
        mainText = findViewById(R.id.text_main);
        messageText = findViewById(R.id.text_message);
        cartText = findViewById(R.id.text_cart);
        profileText = findViewById(R.id.text_profile);

        mainText.setOnClickListener(this);
        messageText.setOnClickListener(this);
        cartText.setOnClickListener(this);
        profileText.setOnClickListener(this);

        tabList.add(mainText);
        tabList.add(messageText);
        tabList.add(cartText);
        tabList.add(profileText);
    }

    /**
     * 更新底部导航栏标签的选中状态
     * @param clickedTabId 被点击标签的ID
     */
    private void tabChange(int clickedTabId) {
        for (TextView tab : tabList) {
            if (tab.getId() == clickedTabId) {
                tab.setBackgroundColor(getColor(R.color.background));
            } else {
                tab.setBackgroundColor(getColor(R.color.white));
            }
        }
    }
}